package com.example.friendsfinder;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.concurrent.Semaphore;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class Login extends Activity {
	
    public final static String 	USERNAME = "com.example.friendsfinder.USERNAME";
    private String 				username;
    private String 				password;	
	private LoginConnection 	loginConnection;
	private boolean 			loginFinished;
	private boolean 			loginOk;
	private Socket 				loginSocket;
	private DataOutputStream 	out; 	/* stream per l'invio dei dati al server */
	private DataInputStream 	in; 	/* stream per la ricezione dei dati dal server */
	private SocketAddress 		sockaddr;
	private final String 		serverhost = "10.0.2.2";
	private final int 			serverport = 1234;
	private static 				Semaphore semaphore;

	public class LoginConnection extends AsyncTask<String, Void, Void> {
		
	    protected Void doInBackground(String... dataLogin) {
	    	
	    	Integer serverPort = Integer.parseInt(dataLogin[1]);
			try {
				/* creazione del SocketAddress e del socket */
				
				sockaddr = new InetSocketAddress(dataLogin[0], serverPort.intValue());
				loginSocket = new Socket();
				loginSocket.setSoTimeout(0);
				loginSocket.connect(sockaddr,10000); /* connessione al server */
					
				out = new DataOutputStream(loginSocket.getOutputStream()); 
				in = new DataInputStream(loginSocket.getInputStream());
				
				

				/* 'L': identificatore del comando login */
				out.writeByte('L');

				/* invio dello username (e del numero di caratteri) */
				out.writeInt(username.length());
				out.writeBytes(username);
				
				/* invio della password (e del numero di caratteri) */
				out.writeInt(password.length());
				out.writeBytes(password);
					
				/* prelievo di un booleano: se false username mancante o password errata*/
				loginOk = in.readBoolean(); 

				out.flush();
				
				out.close();
				in.close();
				loginSocket.close();
					
				semaphore.release();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			return null;
	    }
	};
	

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_login);
        loginConnection = new LoginConnection();
    }

	
	public void loginUser(View view) {
		semaphore = new Semaphore(0, true);

    	Intent intent = new Intent(this, FriendsFinder.class);
    	EditText editUsername = (EditText) findViewById(R.id.edit_username);
    	EditText editPassword = (EditText) findViewById(R.id.edit_password);

    	username = editUsername.getText().toString();
        password = editPassword.getText().toString();
                
		loginConnection.execute(serverhost, String.valueOf(serverport));
				
		try {
			semaphore.acquire();
		}
		catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		/* loginOk==true se username presente && password corretta */
		if(loginOk) {
			intent.putExtra(USERNAME, username);
			startActivity(intent);
		}
		else {
			/* mettere una TextView su Login con scritto USERNAME INESISTENTE O PASSWORD ERRATA */
		}
	}

}
